<?php

class It_UsersController extends Zend_Controller_Action
{
	private $_config;
    public function init ()
    {
        $this->_config = new Zend_Config_Ini(CONFIG_PATH . '/columns_it.ini',APPLICATION_ENV);
        $acl = new My_Acl();
        $auth = $acl->checkAllowed($this->_request);
    }

    public function indexAction ()
    {
        $model_users = new It_Model_Users();
        $this->view->users_list = $model_users->getAllData();
        
        $auth = Zend_Auth::getInstance();
        $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_CMDB_IT'));
        $userInfo = $auth->getIdentity();
        $this->view->users_check = $model_users->getUserByUsername($userInfo['username']);
    }
    public function deleteAction ()
    {
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){
                $userId = $this->getRequest()->getPost('userId');
                $model_users = new It_Model_Users();
                $model_users->deleteUser($userId);
                $model_usersroles = new It_Model_Usersroles();
                $model_usersroles->deleteUserId($userId);
                $model_option = new It_Model_Option();
                $model_option->deleteUserId($userId);
            }
            $this->_helper->redirector('index', 'users', 'it');
        }else{
            $id = $this->_getParam('id', 0);
            
            $auth = Zend_Auth::getInstance();
            $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_CMDB_IT'));
            $userInfo = $auth->getIdentity();
            $model_users = new It_Model_Users();
            $users_check = $model_users->getUserByUsername($userInfo['username']);
            if($users_check->userId == $id){
                $this->_helper->redirector('index', 'users', 'it');
                exit();
            }
            $this->view->user = $model_users->getUserByID($id);
        }
    }
    public function addAction ()
    {
        $form = new It_Form_Users();
        $form->submit->setLabel('Thêm mới!');
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {
                $username   = $form->getValue('username');
                $password   = $form->getValue('password');
                $userRole   = $form->getValue('userRole');
                $status     = $form->getValue('status');
                
				$viewsConfig = $this->_config->views;
                $model_user = new It_Model_Users();
				if(strlen($password) < 6){
					$this->view->mess ="Mật khẩu tối thiểu 6 ký tự!!!";
				}elseif($model_user->getUserByUsername($username)){
					$this->view->mess ="Username đã tồn tại!!!";
				}else{
					$userId = $model_user->addUser($username, $password, $status);
                    //Thêm các quyền của user
                    $count = count($userRole);
                	$dataItem = array();
                	for($i=0;$i<$count;++$i){
                		if(!$userRole[$i]){
                			continue;
                		}
                		$dataItem[] = '('.$userId . ',' . (int)($userRole[$i]).')';
                	}
                    $model_usersroles = new It_Model_Usersroles();
            		$model_usersroles->addMultiData($dataItem);
                    
                    $model_rolesoption = new It_Model_Rolesoption();
                    $view_list = $model_rolesoption->getDataByUserId($userId);
                    $views = array();
                    foreach($view_list as $row){
                        $views = array_merge($views, json_decode($row['views']));
                    }
                    $views = array_unique($views);
                    $views = array_values($views);
                    $views = json_encode($views);
                    
                    $model_option = new It_Model_Option();
                    $model_option->addData($userId,$views);
                            
                	$this->_helper->redirector('index','users', 'it');
				}

            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction()
    {
		$form = new It_Form_Users();
		$form->submit->setLabel( 'Lưu' );
		$this->view->form = $form;

		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {

				$userId = (int)($form->getValue('userId'));
				$username = $form->getValue('username');
				$password = $form->getValue('password');
				$userRole = $form->getValue('userRole');
                $status     = $form->getValue('status');

                $model_users = new It_Model_Users();
                $user_check = $model_users->getUserByUsername($username);
                if(strlen($password) < 6){
					$this->view->mess ="Mật khẩu tối thiểu 6 ký tự!!!";
				}elseif(!$user_check || $user_check['userId'] == $userId){
    				$model_users->updateUser($userId, $username, $password, $status);
                    
                    $model_usersroles = new It_Model_Usersroles();
                    $model_usersroles->deleteUserId($userId);
            
            		$dataRole = array();
            		if(count($userRole) > 0){
            			for($i = 0; $i<count($userRole); $i++){
            				$dataRole[] =	'('. $userId . ',' . $userRole[$i] .')';
            			}
            			$dataRole = array_unique($dataRole);
            
                        $model_usersroles->addMultiData($dataRole);
            		}
				    $this->_helper->redirector('index', 'users', 'it');
                }else{
                    $this->view->mess ="Username đã tồn tại!!!";
    				$form->populate($formData);
    			}

			}else{
				$form->populate($formData);
			}
		}else{
			$id = (int)($this->_getParam('id', 0));
                
            $model_users = new It_Model_Users();
			$users_check = $model_users->getUserByID($id);
            if($users_check){
                $model_roles = new It_Model_Roles();
                $roles_list = $model_roles->getRolesUser($id);
                
    			$this->view->roles_list = $roles_list;
    			$this->view->users_check = $users_check;
    			$form->populate($users_check->toArray());
            }else{
                $this->_helper->redirector('index', 'users', 'it');
            }
		}
    }
}





